package hu.tortavar2.client.activity;

import hu.tortavar2.client.ClientFactory;
import hu.tortavar2.client.SiteService;
import hu.tortavar2.client.SiteServiceAsync;
import hu.tortavar2.client.place.ProductDetailPlace;
import hu.tortavar2.client.view.layout.ProductDetailView;
import hu.tortavar2.client.view.layout.ProductDetailViewImpl;
import hu.tortavar2.shared.Product;

import com.google.gwt.activity.shared.AbstractActivity;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AcceptsOneWidget;

public class ProductDetailActivity extends AbstractActivity implements ProductDetailView.Presenter{

	SiteServiceAsync siteService = GWT.create(SiteService.class);
	
	private final ProductDetailPlace place;
	private final ProductDetailView view;
	private final PlaceController placeController;
	
	public ProductDetailActivity(ProductDetailPlace place, ClientFactory clientFactory) {
		this.place = place;
		view = new ProductDetailViewImpl();
		placeController = clientFactory.getPlaceController();
	}
	
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		view.setPresenter(this);
		panel.setWidget(view.asWidget());
		//refreshProductDetail();
	}

	@Override
	public void goTo(Place place) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onFavouriteClicked(String product) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onBackClicked() {
		// TODO Auto-generated method stub
		History.back();
	}

	@Override
	public void refreshProductDetail() {
		// TODO Auto-generated method stub
		siteService.getProduct(place.getEncodedKey(), new AsyncCallback<Product>() {
			
			@Override
			public void onSuccess(Product result) {
				view.setProductDetail(result);
			}
			
			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
